package com.bsj.travel.def.monitor.tdengine;

import com.bsj.travel.enums.DataTypeEnum;
import com.bsj.travel.util.StringUtils;
import lombok.Data;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author yinhao
 * @version 1.0
 * @description TD时序数据库建表实体
 * @date 2024/1/31
 */
@Data
public class FieldsVo implements Serializable {
    private static final long serialVersionUID = 8653139850694836979L;

    /**
     * 字段名称
     */
    private String fieldName;

    /**
     * 字段数据类型
     */
    private String dataType;

    /**
     * 字段字节大小
     */
    private Integer size;

    /**
     *@MethodDescription 字段实体类转为vo类
     *@param fields 字段实体类
     *@return FieldsVo 字段实体vo类
     */
    public static FieldsVo fieldsTranscoding(Fields fields) throws SQLException {
        if (StringUtils.isBlank(fields.getFieldName()) || fields.getDataType() == null) {
            throw new SQLException("invalid operation: fieldName or dataType can not be null");
        }
        FieldsVo fieldsVo = new FieldsVo();
        fieldsVo.setFieldName(fields.getFieldName());
        fieldsVo.setDataType(fields.getDataType().getDataType());
        fieldsVo.setSize(fields.getSize());
        return fieldsVo;
    }

    /**
     *@MethodDescription 字段实体类集合转为vo类集合
     *@param fieldsList 字段实体类集合
     *@return List<FieldsVo> 字段实体vo类集合
     */
    public static List<FieldsVo> fieldsTranscoding(List<Fields> fieldsList) throws SQLException{
        List<FieldsVo> fieldsVoList = new ArrayList<>();
        for (Fields fields : fieldsList) {
            fieldsVoList.add(fieldsTranscoding(fields));
        }
        return fieldsVoList;
    }

    /**
     * 日志超级表创建
     * @return
     * @throws SQLException
     */
    public static SuperTable fieldsTransLog(String superTableName) throws SQLException{
        SuperTable superTable = new SuperTable();
        //字段
        List<Fields> fieldsList1 = List.of(Fields.builder()
                .fieldName("ts").dataType(DataTypeEnum.TIMESTAMP).build(),
                Fields.builder()
                        .fieldName("traceId").dataType(DataTypeEnum.NCHAR).build(),
                Fields.builder()
                        .fieldName("serveType").dataType(DataTypeEnum.INT).build(),
                Fields.builder()
                        .fieldName("operate").dataType(DataTypeEnum.NCHAR).size(65).build(),
                Fields.builder()
                        .fieldName("code").dataType(DataTypeEnum.INT).build());
        List<FieldsVo> schemaFields = new ArrayList<>();
        for (Fields fields : fieldsList1) {
            schemaFields.add(fieldsTranscoding(fields));
        }
        //标签
        List<Fields> fieldsList2 = List.of(Fields.builder()
                //该标签表示消息的上下行 1设备消息上行 2平台消息下行
                        .fieldName("msgType").dataType(DataTypeEnum.INT).build());
        List<FieldsVo> tagsFields = new ArrayList<>();
        for (Fields fields : fieldsList2) {
            tagsFields.add(fieldsTranscoding(fields));
        }
        superTable.setTagsFields(tagsFields);
        superTable.setSuperTableName(superTableName);
        return superTable;
    }
}
